package com.yoda.mall.admin.controller;

import com.yoda.mall.admin.service.ProductAttributeService;
import com.yoda.mall.model.entity.pms.Brand;
import com.yoda.mall.model.entity.pms.ProductAttribute;
import com.yoda.mall.model.entity.pms.ProductAttributeCategory;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.ProductAttributeVo;
import com.yoda.mall.model.vo.R;
import org.bouncycastle.pqc.crypto.newhope.NHOtherInfoGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangjia
 * @date 2022/12/17
 * @Description
 */
@RestController
@RequestMapping("/productAttribute")
public class ProductAttributeController {

    @Autowired
    private ProductAttributeService productAttributeService;


    //添加品牌属性
    @PostMapping("/category/create")
    public R addProduct(@RequestParam String name) {
        productAttributeService.addProduct(name);
        return R.success("添加成功");
    }

    //根据id删除品牌属性
    @GetMapping("/category/delete/{id}")
    public R delete(@PathVariable(value = "id") Long id) {
        productAttributeService.delete(id);
        return R.success("删除成功");
    }

    //分页获取所有商品属性分类
    @GetMapping("/category/list")
    public R list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5")
            Integer pageSize) {
        PageResult<ProductAttributeCategory> pageResult = productAttributeService.list(pageNum, pageSize);
        return R.success(pageResult);
    }

    //根据id查询
    @GetMapping("/category/{id}")
    public R findById(@PathVariable(value = "id") Long id) {
        ProductAttributeCategory category = productAttributeService.findById(id);
        return R.success(category);
    }

    //修改
    @PostMapping("/category/update/{id}")
    public R updateBrand(@PathVariable(value = "id") Long id, @RequestParam String name) {
        productAttributeService.update(id, name);
        return R.success("更新成功");
    }

    //获取所有商品属性分类及其下属性
    @GetMapping("/category/list/withAttr")
    public R listWithAttr() {
        List<ProductAttributeVo> vo = productAttributeService.findProduct();
        return R.success(vo);
    }

    //获取所有商品属性分类及其下属性
    @GetMapping("/attrInfo/{id}")
    public R getCategoryById(@PathVariable Long id) {
        ProductAttributeCategory category = productAttributeService.findById(id);
        return R.success(category);
    }

    //添加商品属性信息
    @PostMapping("/create")
    public R create(@RequestBody ProductAttribute attribute) {
        productAttributeService.create(attribute);
        return R.success("添加成功");
    }

    //批量删除属性
    @PostMapping("/{ids}")
    public R deleteIds(@RequestParam("ids") List<Integer> ids) {
        productAttributeService.deleteIds(ids);
        return R.success("删除成功");
    }

    //根据id查询商品属性
    @GetMapping("/{id}")
    public R findOne(@PathVariable(value = "id") Long id) {
        ProductAttribute attribute = productAttributeService.findOne(id);
        return R.success(attribute);
    }

    //根据分类查询属性列表或参数列表
    @GetMapping("/list/{cid}")
    public R findPage(@PathVariable(value = "cid")Long cid,@RequestParam Integer type,@RequestParam(defaultValue = "1") Integer pageNum,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
       PageResult pageResult =  productAttributeService.findPage(cid,type,pageNum,pageSize);
       return R.success(pageResult);
    }

    //根据商品分类id获取商品属性及属性分类
    @GetMapping("/attrInfo/{productCategoryId")
    public R attrInfo(@PathVariable(value = "productCategoryId") Long productCategoryId) {
       ProductAttribute attribute = productAttributeService.findAttribute(productCategoryId);
       return R.success(attribute);
    }
}